package com.wh.blockquene.blockingQueue;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;


public class ArrayBlockingQueueTest {

	public static void main(String[] args) throws InterruptedException {
		ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);
		Thread a = new Thread(new MessageProvider(queue));
		Thread b = new Thread(new MessageCustomer(queue));
		a.start();
		b.start();
	}
	
}

/**
 * 
 *  Class Name: ArrayBlockingQueueTest.java
 *  Description: 生产者线程
 *  @author wanghao  DateTime 2016年12月31日 下午4:55:27 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
class MessageProvider implements Runnable {

	private ArrayBlockingQueue<String> queue;
	
	
	public MessageProvider(ArrayBlockingQueue<String> queue) {
		super();
		this.queue = queue;
	}

	@Override
	public void run() { 
		while(true) {
			Random random = new Random();
			String msg = "产生消息:" + random.nextInt(10);
			try {
				queue.put(msg);
				System.out.println(msg);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}

/**
 * 
 *  Class Name: ArrayBlockingQueueTest.java
 *  Description: 消费者线程
 *  @author wanghao  DateTime 2016年12月31日 下午4:56:06 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
class MessageCustomer implements Runnable {
	
	private ArrayBlockingQueue<String> queue;
	
	public MessageCustomer(ArrayBlockingQueue<String> queue) {
		super();
		this.queue = queue;
	}
	
	@Override
	public void run() {
		while(true) {
			try {
				String msg =  queue.take();
				System.out.println("消费接收的消息：" + msg);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}